﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.Globalization;
using System.Runtime.InteropServices;

namespace Tesla.Collections {
    /// <summary>
    /// A key for two pieces of data that are linked together in some manner.
    /// </summary>
    /// <typeparam name="T">First data type</typeparam>
    /// <typeparam name="G">Second data type</typeparam>
    [Serializable]
    [StructLayout(LayoutKind.Sequential)]
    public struct TwoTupleKey<T,G> : IEquatable<TwoTupleKey<T,G>>, IComparable<TwoTupleKey<T,G>> {
        private T _first;
        private G _second;

        /// <summary>
        /// Gets the first piece of data the key represents.
        /// </summary>
        public T First {
            get {
                return _first;
            }
        }

        /// <summary>
        /// Gets the second piece of data the key represents.
        /// </summary>
        public G Second {
            get {
                return _second;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="TwoTupleKey&lt;T, G&gt;"/>.
        /// </summary>
        /// <param name="first">The first piece of data.</param>
        /// <param name="second">The second piece of data.</param>
        public TwoTupleKey(T first, G second) {
            _first = first;
            _second = second;
        }

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        ///   <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj) {
            if(obj != null && obj.GetType() == GetType()) {
                return Equals((TwoTupleKey<T, G>) obj);
            }
            return false;
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public bool Equals(TwoTupleKey<T, G> other) {
            if(other.First.GetHashCode() == _first.GetHashCode()
                && other.Second.GetHashCode() == _second.GetHashCode()) {
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance.
        /// </returns>
        public override int GetHashCode() {
            int hash = _first.GetHashCode();
            hash = ((hash << 5) - hash) + _second.GetHashCode();
            return hash;
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString() {
            CultureInfo info = CultureInfo.CurrentCulture;
            return String.Format(info, "{{1} , {2}}}",
                new Object[] { _first.ToString(), _second.ToString()});
        }

        /// <summary>
        /// Compares the current object with another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings:
        /// Value
        /// Meaning
        /// Less than zero
        /// This object is less than the <paramref name="other"/> parameter.
        /// Zero
        /// This object is equal to <paramref name="other"/>.
        /// Greater than zero
        /// This object is greater than <paramref name="other"/>.
        /// </returns>
        public int CompareTo(TwoTupleKey<T, G> other) {
            int otherFirst = other.First.GetHashCode();
            int otherSecond = other.Second.GetHashCode();

            int first = _first.GetHashCode();
            int second = _second.GetHashCode();

            if(first < otherFirst) {
                //Less than, this should always come before
                return -1;
            } else if(first == otherFirst) {
                //Equal, need to check second value
                if(second < otherSecond) {
                    //Less than, this should always come before
                    return -1;
                } else if(second == otherSecond) {
                    //All equal, return 0
                    return 0;
                } else {
                    //Greater than, this should always come after
                    return 1;
                }
            } else {
                //Greater than, this should always come after
                return 1;
            }
        }
    }
}
